package node.servlet;

import hibernate.mappings.MsNode;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import node.dao.CommonDao;
import util.BaseServlet;
import util.CacheUtil;
import util.Java2JsonUtil;

public class NodeServlet extends BaseServlet {

	protected void doBusiness() {
		String sort = request.getParameter("sort");
		System.out.println("sort:" + sort);
		if ("nodelist".equals(sort)) {
			nodelist();
		} else if ("nodetree".equals(sort)) {
			nodetree();
		} else if ("addNode".equals(sort)) {
			addNode();
		} else if ("nodeListData".equals(sort)) {
			nodeListData();
		}

	}

	private void addNode() {
		CacheUtil cu = new CacheUtil();
		String nodeId = request.getParameter("nodeId");
		String parentId = request.getParameter("parentId");
		String nodetitle = request.getParameter("nodetitle");
		String ntype = request.getParameter("ntype");
		String npub = request.getParameter("npub");
		String tableName = request.getParameter("tableName");
		String dpub = request.getParameter("dpub");
		MsNode ms = new MsNode();
		if (nodeId == null || nodeId.equals("")) {
			ms.setParent(Integer.parseInt(parentId));
		} else {
			int id = Integer.parseInt(nodeId);
			List<MsNode> nodeList = cu.getNodeList();
			for (int i = 0; i < nodeList.size(); i++) {
				ms = nodeList.get(i);
				if (ms.getId() == id) {
					break;
				}
			}
		}
		ms.setTitle(nodetitle);
		ms.setNtype(Integer.parseInt(ntype));
		ms.setNpub(Integer.parseInt(npub));
		ms.setTablename(tableName);
		ms.setDpub(Integer.parseInt(dpub));
		new CommonDao().saveOrUpdateEntityForH(ms);
		cu.freshNodeList();
		Map m = new HashMap();
		m.put("success", true);
		m.put("msg", "保存成功");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.println(Java2JsonUtil.toJson(m));
		out.close();
	}

	private void nodelist() {
		String id = request.getParameter("id");
		String type = request.getParameter("type");
		String title = "";
		try {
			title = new String(request.getParameter("title").getBytes(
					"ISO-8859-1"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("title", title);
		// request.setAttribute("nodeId", id);
		String page = "";
		if ("0".equals(type)) {// node
			page = "node/nodeList.jsp";
		} else if ("1".equals(type)) {// doc
			page = "doc/docList.jsp";
		} else if ("2".equals(type)) {

		}
		try {
			request.getRequestDispatcher(page).forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void nodetree() {
		String node_id = request.getParameter("node_id");
		int parent = 0;
		String s = "";
		if ("0".equals(node_id)) {// 根节点0
		} else {
			parent = Integer.parseInt(node_id);
		}
		CacheUtil cu = new CacheUtil();
		List<MsNode> nodeList = cu.getNodeList();
		List list = new ArrayList();
		for (int i = 0; i < nodeList.size(); i++) {
			if (nodeList.get(i).getParent() == parent) {
				list.add(nodeList.get(i));
			}
		}
		// List list = new NodeDao().getNodeList(parent);
		MsNode n = new MsNode();
		for (int i = 0; i < list.size(); i++) {
			n = (MsNode) list.get(i);
			s = s + ",{id:" + n.getId() + ",text:'" + n.getTitle()
					+ "',href:'NodeServlet?sort=nodelist&id=" + n.getId()
					+ "&title=" + n.getTitle() + "&type=" + n.getNtype() + "'";
			if (0 == n.getNtype()) {
				s = s + ",cls: 'folder'}";
			} else {
				s = s + ",cls: 'file',leaf: true}";
			}
		}
		s = s.replaceFirst(",", "");
		s = "[" + s + "]";
		response.setCharacterEncoding("utf-8");
		try {
			response.getWriter().write(s);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void nodeListData() {
		String nodeId = request.getParameter("nodeId");
		int nid = Integer.parseInt(nodeId);
		List<MsNode> list = new ArrayList();
		List<MsNode> nodeList = new CacheUtil().getNodeList();
		for (int i = 0; i < nodeList.size(); i++) {
			if (nid == nodeList.get(i).getParent()) {
				list.add(nodeList.get(i));
			}
		}
		Map m = new HashMap();
		m.put("root", list);
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.println(Java2JsonUtil.toJson(m));
		out.close();
	}
}
